@extends('layouts.base')
@section('title', '订单管理')
@section('admin-style')
    <link rel="stylesheet" href="{{ asset('/sj/css/datepicker.css') }}">
    <script src="{{ asset('/sj/js/plugins/moment.min.js') }}"></script>
    <script src="{{ asset('/sj/js/datepicker.all.js') }}"></script>
@stop
@section('content')
    <!-- ============================================================== -->
    <!-- Container fluid  -->
    <!-- ============================================================== -->
    <div class="container-fluid">
        <!-- ============================================================== -->
        <!-- Bread crumb and right sidebar toggle -->
        <!-- ============================================================== -->
        <div class="row page-titles">
            <div class="col-md-5 align-self-center">
                <h3 class="text-themecolor">订单管理--列表</h3>
            </div>
            <div class="col-md-7 align-self-center">
                <ol class="breadcrumb">
                    <li class="breadcrumb-item"><a href="javascript:void(0)">首页</a></li>
                    <li class="breadcrumb-item">订单管理</li><li class="breadcrumb-item active">订单列表</li>
                </ol>
            </div>
        </div>
        <!-- ============================================================== -->
        <!-- End Bread crumb and right sidebar toggle -->
        <!-- ============================================================== -->
        <!-- ============================================================== -->
        <!-- Start Page Content -->
        <!-- ============================================================== -->
        <div class="row">
            <div class="col-12">
                <div class="card">
                    <div class="card-body">
                        <h4 class="card-title">订单列表</h4>
                        <h6 class="card-subtitle"></h6>
                        <div class="card-body" >
                            <form method="get" action="{{ route('admin.order.index') }}" >
                                <div class="form-row">
                                    <div class="col-md-3 mb-3">
                                        <label for="validationDefault04">订单编号</label>
                                        <input type="text" name="orderSn" class="form-control" value="{{request()->orderSn}}" placeholder="订单编号">
                                    </div>
                                    <div class="col-md-3 mb-3">
                                        <label for="validationDefault04">设备编号</label>
                                        <input type="text" name="machinesImel" class="form-control" value="{{request()->machinesImel}}" placeholder="设备编号">
                                    </div>
                                    <div class="col-md-3 mb-3">
                                        <label for="validationDefault04">设备名称</label>
                                        <input type="text" name="machinesName" class="form-control" value="{{request()->machinesName}}" placeholder="设备名称">
                                    </div>
                                    <div class="col-md-3 mb-3">
                                        <label for="validationDefault04">商户名称</label>
                                        <input type="text" name="adminName" class="form-control" autocomplete="off" value="{{request()->adminName}}" placeholder="商品名称">
                                    </div>
                                    <div class="col-md-3 mb-3">
                                      <div class="mt40">
                                          <div>订单时间</div>
                                          <div class="c-datepicker-date-editor c-datepicker-single-editor J-datepicker-day mt10">
                                            <i class="c-datepicker-range__icon kxiconfont icon-clock"></i>
                                            <input type="text" autocomplete="off" name="startTime" placeholder="选择开始时间" class=" c-datepicker-data-input only-date" value="{{request()->startTime}}">
                                          </div>
                                          <span class="c-datepicker-range-separator">--</span>
                                          <div class="c-datepicker-date-editor c-datepicker-single-editor J-datepicker-day mt10">
                                            <i class="c-datepicker-range__icon kxiconfont icon-clock"></i>
                                            <input type="text" autocomplete="off" name="endTime" placeholder="选择结束时间" class=" c-datepicker-data-input only-date" value="{{request()->endTime}}">
                                        </div>
                                      </div>
                                    </div>
                                    <div class="col-md-3 mb-3">
                                      <label for="validationDefault04">支付方式</label>
                                      <select class="form-control custom-select" name="payment">
                                          <option value="">全部</option>
                                          <option value="1" @if(request()->payment == 1) selected @endif>微信</option>
                                          <option value="2" @if(request()->payment == 2) selected @endif>支付宝</option>
                                      </select>
                                    </div>
                                    <div class="col-md-3 mb-3">
                                        <label for="validationDefault04">支付状态</label>
                                        <select class="form-control custom-select" name="orderState">
                                            <option value="">全部</option>
                                            <option value="2" @if(request()->orderState == 2) selected @endif>成功</option>
                                            <option value="1" @if(request()->orderState == 1) selected @endif>未成功</option>
                                            <option value="99" @if(request()->orderState == 99) selected @endif>已退款</option>
                                        </select>
                                    </div>
                                    <div class="col-md-3 mb-3">
                                        <label for="validationDefault04">商品名称</label>
                                        <input type="text" name="goodsName" class="form-control" value="{{request()->goodsName}}" placeholder="商品名称">
                                    </div>
                                    
                                </div>
                                <button class="btn btn-primary" type="submit" style="margin-left: 480px;">筛选</button>
                                <a href="{{ route('admin.order.index') }}" class="btn btn-info">重置</a>
                                <!-- <a class="btn btn-success float-right" href="{{ route('admin.order.putOrderExcel') }}"></i> 导出订单</a> -->
                                <a class="btn btn-success float-right" href="javascript:;" onclick="searchexport(this)"></i> 导出订单</a>
                            </form>
                        </div>

                        <div class="card">
                            <div class="card-body">
                            <h4 class="card-title">销售统计</h4>
                                <div class="tablesaw-bar tablesaw-mode-columntoggle">
                                  <div class="tablesaw-columntoggle-btnwrap tablesaw-advance">
                                    <table class="tablesaw table-striped table-hover table-bordered table tablesaw-columntoggle" style="width:0%;">
                                    <thead>
                                        <tr>
                                            <th style="color: #000000;background: #ffffff;border-color: #000000;"></th>
                                            <th style="color: #000000;background: #ffffff;border-color: #000000;">已收款</th>
                                            <th style="color: #000000;background: #ffffff;border-color: #000000;">已退款</th>
                                            <th style="color: #000000;background: #ffffff;border-color: #000000;">余款</th>
                                            <th style="color: #000000;background: #ffffff;border-color: #000000;">订单总数</th>
                                        </tr>
                                    </thead>
                                    <tbody>
                                        <tr>
                                            <td class="tablesaw-priority-2">微信</td>
                                            <td class="tablesaw-priority-3">￥@if(!$wxrefStatusa)0.00 @else{{$wxrefStatusa}}@endif</td>
                                            <td class="tablesaw-priority-2">￥@if(!$wxrefStatusb)0.00 @else{{$wxrefStatusb}}@endif</td>
                                            <td class="tablesaw-priority-1">￥@if(!$wxrefStatusc)0.00 @else{{$wxrefStatusa-$wxrefStatusb}}@endif</td>
                                            <td class="tablesaw-priority-1">{{$wxtotalPrices}}（单）</td>
                                        </tr>
                                        <tr>
                                            <td class="tablesaw-priority-2">支付宝</td>
                                            <td class="tablesaw-priority-3">￥@if(!$zfrefStatusa)0.00 @else{{$zfrefStatusa}}@endif</td>
                                            <td class="tablesaw-priority-2">￥@if(!$zfrefStatusb)0.00 @else{{$zfrefStatusb}}@endif</td>
                                            <td class="tablesaw-priority-1">￥@if(!$zfrefStatusc)0.00 @else{{$zfrefStatusa-$zfrefStatusb}}@endif</td>
                                            <td class="tablesaw-priority-1">{{$zfbtotalPrices}}（单）</td>
                                        </tr>
                                        <tr>
                                            <td class="tablesaw-priority-2">合计：</td>
                                            <td class="tablesaw-priority-3">￥@if(!$totalPricea)0.00 @else{{$wxrefStatusa+$zfrefStatusa}}@endif</td>
                                            <td class="tablesaw-priority-2">￥@if(!$totalPriceb)0.00 @else{{$wxrefStatusb+$zfrefStatusb}}@endif</td>
                                            <td class="tablesaw-priority-1">￥@if(!$totalPricec)0.00 @else{{$wxrefStatusc+$zfrefStatusc}}@endif</td>
                                            <td class="tablesaw-priority-1">总订单数：{{$totalPrices}}（单）</td>
                                        </tr>
                                        
                                    </tbody>
                                </table>
                            </div>
                        </div>
                        <div class="card">
                          <div class="card-body">
                              <div class="table-responsive">
                                  <table id="demo-foo-row-toggler" class="table table-bordered footable footable-1 breakpoint breakpoint-lg" data-toggle-column="first" style="">
                                      <thead>
                                        <tr style="background: #000000;">
                                          <th width="5%">序号</th>
                                          <th width="2%">订单编号</th>
                                          <th width="5%">设备编码</th>
                                          <th width="5%">货道编号</th>
                                          <th width="5%">设备名称</th>
                                          <th width="5%">商户名称</th>
                                          <th width="5%">商品名称</th>
                                          <th width="5%">商品图片</th>
                                          <th width="5%">商品单价</th>
                                          <th width="5%">支付类型</th>
                                          <th width="5%">支付时间</th>
                                          <th width="5%">订单状态</th>
                                          <th width="5%">退款状态</th>
                                          <th width="5%">下单时间</th>
                                          <th width="5%">操作</th>
                                        </tr>
                                      </thead>
                                      <tbody> 
                                        @foreach($result as $k=>$item)
                                          <tr class="footable-detail-row">
                                            <td colspan="14">
                                              <table class="footable-details table table-bordered">
                                                  <tbody>
                                                    @if($k == 0)
                                                      {{-- @if($item->order_snp != NUll && $item->order_snp == $result[$k+1]->order_snp) --}}
                                                      @if($item->order_snp != NUll)
                                                      <tr>下单时间：{{ $item->created_at}}  总订单号：{{ $item->order_snp}}</tr>
                                                      @endif
                                                    @else
                                                      @if($item->order_snp != NUll && $item->order_snp != $result[$k-1]->order_snp)
                                                      <tr>下单时间：{{ $item->created_at}}  总订单号：{{ $item->order_snp}}</tr>
                                                      @endif
                                                    @endif
                                                  </tbody>
                                              </table>
                                            </td>
                                          </tr> 
                                          <tr data-expanded="true">
                                            <td class="footable-first-visible" style="display: table-cell;">{{($result->currentPage()-1)*$result->perPage()+$k+1}}</td>
                                            <td style="display: table-cell;width:5%;">{{ $item->ordersn}}</td>
                                            <td style="display: table-cell;">{{ $item->machines_imel}}</td>
                                            <td style="display: table-cell;">{{ $item->freightway_id}}</td>
                                            <td style="display: table-cell;">@if($item->machines_name){{ $item->machines_name}}@else暂无设备名称 @endif</td>
                                            <td style="display: table-cell;">@if($item->name){{ $item->name}}@else暂无商户名称 @endif</td>
                                            <td style="display: table-cell;">@if($item->goodsname){{ $item->goodsname}}@else暂无商品名称 @endif</td>
                                            <td style="display: table-cell;">@if($item->goods_image)<img src="{{ $item->domainName}}{{$item->goods_image}}" style="width:50px;height:50px;">@else暂无图片@endif</td>
                                            <td style="display: table-cell;">{{ $item->goods_total_price}}</td>
                                            <td style="display: table-cell;">{{ $item->payment}}</td>
                                            <td style="display: table-cell;">{{ $item->payment_time }}</td>
                                            @if($item->order_status == 2)
                                              <td style="display: table-cell;">有效订单---<span>{{ $item->fault}}</span></td>
                                            @elseif($item->order_status == 0)
                                              <td style="display: table-cell;">无效订单---<span>未支付</span></td>
                                            @else
                                              <td style="display: table-cell;">无效订单---<span>{{ $item->fault}}</span></td>
                                            @endif
                                            @if($item->refund_status == 1)
                                            <td style="display: table-cell;">等待退款</span></td>
                                            @elseif($item->refund_status == 2)
                                            <td style="display: table-cell;">退款成功</span></td>
                                            @else
                                            <td style="display: table-cell;">未退款</span></td>
                                            @endif
                                            <td style="display: table-cell;">{{ $item->created_at}}</td>
                                            @can('admin.orders.refund')
                                            <td>
                                              @if(in_array($item->order_status,[1,3,4]) && $item->refund_status == 0 && (time()-strtotime($item->payment_time) < 6*3600))
                                              <a onclick="refunds('{{$item->id}}')" href="javascript:;" class="btn btn-success float-right btn-apply-refund">退款</a>
                                              @elseif($item->refund_status == 2)
                                              退款完成
                                              @endif
                                            </td>
                                            @endcan
                                          </tr>
                                          @endforeach      
                                      </tbody>
                                  </table>
                              </div>
                          </div>
                        </div>
                    </div>
                    <div class="row">
                        <div class="col-sm-12 col-md-4"><div class="dataTables_info" id="example_info" role="status" aria-live="polite"></div></div>
                        <div class="col-sm-12 col-md-8">
                            <div class="dataTables_paginate paging_simple_numbers" id="example_paginate">
                                {{$result->appends([
                                    'goodsName' => request()->goodsName,
                                    'machinesName' => request()->machinesName,
                                    'machinesImel' => request()->machinesImel,
                                    'orderState' => request()->orderState,
                                    'refundStatus' => request()->refundStatus,
                                    'orderSn' => request()->orderSn,
                                    'startTime' => request()->startTime,
                                    'endTime' => request()->endTime,
                                    'startTime' => request()->startTime,
                                    'categoryId' => request()->categoryId,
                                    'adminName' => request()->adminName
                                ])->links('layouts.pagination')}}
                            </div>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>
@stop
@section('admin-js')

<script type="text/javascript">
    function refunds(id) {
      //alert(orderid);return false;
      
      swal({
			  title: "您确定要退款吗?",
        icon: "warning",
        buttons: ['取消', '确定'],
        dangerMode: true,
			})
			.then((willDelete) => {
			  if (willDelete) {			    
			    $.ajax({
          url: "/admin/order/refoundOrder",
          type: "post",
          dataType: 'json',
          data: {id: id},
          headers: {
              'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
          },
          success: function (data, ret) {
            if (data.code == 0) {
              alert("退款成功");
            } else {
              alert("退款失败");
              return false;
            }
          },
         });
        }
      });
      //error: fun   
    }                                   
</script>
<script type="text/javascript">
      $(function(){
        var DATAPICKERAPI = {
          // 默认input显示当前月,自己获取后填充
          activeMonthRange: function () {
            return {
              startTime: moment().set({ 'date': 1, 'hour': 0, 'minute': 0, 'second': 0 }).format('YYYY-MM-DD HH:mm:ss'),
              endTime: moment().set({ 'hour': 11, 'minute': 59, 'second': 59 }).format('YYYY-MM-DD HH:mm:ss')
            }
          },
          shortcutMonth: function () {
            // 当月
            var nowDay = moment().get('date');
            var prevMonthFirstDay = moment().subtract(1, 'months').set({ 'date': 1 });
            var prevMonthDay = moment().diff(prevMonthFirstDay, 'days');
            return {
              now: '-' + nowDay + ',0',
              prev: '-' + prevMonthDay + ',-' + nowDay
            }
          },
          // 注意为函数：快捷选项option:只能同一个月份内的
          rangeMonthShortcutOption1: function () {
            var result = DATAPICKERAPI.shortcutMonth();
            return [{
              name: '昨天',
              day: '-1,-1',
              time: '00:00:00,23:59:59'
            }, {
              name: '这一月',
              day: result.now,
              time: '00:00:00,'
            }, {
              name: '上一月',
              day: result.prev,
              time: '00:00:00,23:59:59'
            }];
          },
          // 快捷选项option
          rangeShortcutOption1: [{
            name: '最近一周',
            day: '-7,0'
          }, {
            name: '最近一个月',
            day: '-30,0'
          }, {
            name: '最近三个月',
            day: '-90, 0'
          }],
          singleShortcutOptions1: [{
            name: '今天',
            day: '0'
          }, {
            name: '昨天',
            day: '-1',
            time: '00:00:00'
          }, {
            name: '一周前',
            day: '-7'
          }]
        };
          //十分秒年月日单个
          $('.J-datepicker').datePicker({
            hasShortcut:true,
            min:'2018-01-01 04:00:00',
            max:'2019-04-29 20:59:59',
            shortcutOptions:[{
              name: '今天',
              day: '0'
            }, {
              name: '昨天',
              day: '-1',
              time: '00:00:00'
            }, {
              name: '一周前',
              day: '-7'
            }],
            
          });
          
          //年月日单个
          $('.J-datepicker-day').datePicker({
            hasShortcut: true,
            format:'YYYY-MM-DD',
            shortcutOptions: [{
              name: '今天',
              day: '0'
            }, {
              name: '昨天',
              day: '-1'
            }, {
              name: '一周前',
              day: '-7'
            }]
          });
          
          //年月日范围
          $('.J-datepicker-range-day').datePicker({
            hasShortcut: true,
            format: 'YYYY-MM-DD',
            isRange: true,
            shortcutOptions: DATAPICKERAPI.rangeShortcutOption1
          });

          //十分年月日单个
          $('.J-datepickerTime-single').datePicker({
            format: 'YYYY-MM-DD HH:mm'
          });
          
          //十分年月日范围
          $('.J-datepickerTime-range').datePicker({
            format: 'YYYY-MM-DD HH:mm',
            isRange: true
          });
          
          //十分秒年月日范围，包含最大最小值
          $('.J-datepicker-range').datePicker({
            hasShortcut: true,
            min: '2018-01-01 06:00:00',
            max: '2019-04-29 20:59:59',
            isRange: true,
            shortcutOptions: [{
              name: '昨天',
              day: '-1,-1',
              time: '00:00:00,23:59:59'
            },{
              name: '最近一周',
              day: '-7,0',
              time:'00:00:00,'
            }, {
              name: '最近一个月',
              day: '-30,0',
              time: '00:00:00,'
            }, {
              name: '最近三个月',
              day: '-90, 0',
              time: '00:00:00,'
            }]
          });
          //十分秒年月日范围，限制只能选择同一月，比如2018-10-01到2018-10-30
          $('.J-datepicker-range-betweenMonth').datePicker({
            isRange: true,
            between:'month',
            hasShortcut: true,
            shortcutOptions: DATAPICKERAPI.rangeMonthShortcutOption1()
          });
          
          //十分秒年月日范围，限制开始结束相隔天数小于30天
          $('.J-datepicker-range-between30').datePicker({
            isRange: true,
            between: 30
          });
          
          //年月单个
          $('.J-yearMonthPicker-single').datePicker({
            format: 'YYYY-MM',
            min: '2018-01',
            max: '2019-04'
          });
          
          //选择年
          $('.J-yearPicker-single').datePicker({
            format: 'YYYY',
            min: '2018',
            max: '2020'
          });
          
          
      });

      function searchexport(){
        var params = '';
        var start_time = $("input[name='startTime']").val();
        var end_time = $("input[name='endTime']").val();

        params += 'stime='+start_time;
        params += '&etime='+end_time;
        location.href = "{{ route('admin.order.putOrderExcel') }}"+'?'+params;
      }
    </script>
@stop
